package com.landinfo.service.impl;

import com.landinfo.service.AppVersionManageService;
import com.landinfo.domain.AppVersionManage;
import com.landinfo.repository.AppVersionManageRepository;
import com.landinfo.service.dto.AppVersionManageDTO;
import com.landinfo.service.mapper.AppVersionManageMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing AppVersionManage.
 */
@Service
@Transactional
public class AppVersionManageServiceImpl implements AppVersionManageService{

    private final Logger log = LoggerFactory.getLogger(AppVersionManageServiceImpl.class);

    private final AppVersionManageRepository appVersionManageRepository;

    private final AppVersionManageMapper appVersionManageMapper;

    public AppVersionManageServiceImpl(AppVersionManageRepository appVersionManageRepository, AppVersionManageMapper appVersionManageMapper) {
        this.appVersionManageRepository = appVersionManageRepository;
        this.appVersionManageMapper = appVersionManageMapper;
    }

    /**
     * Save a appVersionManage.
     *
     * @param appVersionManageDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public AppVersionManageDTO save(AppVersionManageDTO appVersionManageDTO) {
        log.debug("Request to save AppVersionManage : {}", appVersionManageDTO);
        AppVersionManage appVersionManage = appVersionManageMapper.toEntity(appVersionManageDTO);
        appVersionManage = appVersionManageRepository.save(appVersionManage);
        return appVersionManageMapper.toDto(appVersionManage);
    }

    /**
     *  Get all the appVersionManages.
     *
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public List<AppVersionManageDTO> findAll() {
        log.debug("Request to get all AppVersionManages");
        return appVersionManageRepository.findAll().stream()
            .map(appVersionManageMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     *  Get one appVersionManage by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public AppVersionManageDTO findOne(Long id) {
        log.debug("Request to get AppVersionManage : {}", id);
        AppVersionManage appVersionManage = appVersionManageRepository.findOneById(id);
        return appVersionManageMapper.toDto(appVersionManage);
    }

    /**
     *  Delete the  appVersionManage by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete AppVersionManage : {}", id);
        appVersionManageRepository.deleteById(id);
    }

    /**
     * 获取最新版本
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public AppVersionManageDTO getLatestVersion(){
        List<AppVersionManage> appVersionManages = appVersionManageRepository.findLatest();
        if(appVersionManages != null && appVersionManages.size() > 0){
            AppVersionManage appVersionManage = appVersionManages.get(0);
            return appVersionManageMapper.toDto(appVersionManage);
        }else {
            return null;
        }
    }
}
